import router from '@/router'

import store from '@/store/'

// 进度条样式
import Nprogress from 'nprogress'

import 'nprogress/nprogress.css'

// 设置白名单
const wridePage = ['/login', '/404']

router.beforeEach(async(to, from, next) => {
  // to and from are both route objects. must call `next`.
  Nprogress.start()
  if (store.state.user.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      if (store.state.user.userInfo.userId) {
        next()
      } else {
        await store.dispatch('user/getUserInfo')
        next()
      }
    }
  } else {
    if (wridePage.includes(to.path.toLowerCase())) {
      next()
    } else {
      next('/login')
    }
  }
  Nprogress.done()
})

router.afterEach(() => {
  Nprogress.done()
})
